package com.example.blog.controller;

import com.example.blog.dto.CommentDTO;
import com.example.blog.pojo.PageBean;
import com.example.blog.pojo.Result;
import com.example.blog.service.CommentService;
import com.example.blog.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

//    获取文章的所有评论
    @GetMapping("/list")
    public Result<PageBean<CommentDTO>> list(Integer pageNum,
                                             Integer pageSize,
                                             @RequestParam("id")Integer articleid){

        PageBean<CommentDTO> list = commentService.getlistbyarticleid(pageNum, pageSize, articleid);

        return Result.success(list);
    }

//    获取用户所有的评论
    @GetMapping("/usercomment")
    public Result<List<CommentDTO>> list(){

        List<CommentDTO> list = commentService.getlistbyuserid();

        return Result.success(list);
    }

//    添加评论
    @PostMapping("/add")
    public Result addcomment(@RequestParam("id") Integer articleid, @RequestParam("content") String content){

        boolean insert = commentService.addcomment(articleid,content);
        if(insert) return Result.success();
        else return Result.error("添加失败!");

    }

    //删除评论
    @DeleteMapping("/delete")
    public Result deletecomment(@RequestParam("id")Integer id){

        //获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        boolean s = commentService.deletecomment(id,userid);
        if(s)return Result.success();
        else return Result.error("删除失败!");
    }
}
